from django.contrib import admin
from .models import (
    AdSpace, Advertisement, AdClick, 
    AdImpression, AdCampaign, AdPricing
)


class AdClickInline(admin.TabularInline):
    model = AdClick
    extra = 0
    readonly_fields = ['user', 'ip_address', 'created_at']
    can_delete = False


class AdImpressionInline(admin.TabularInline):
    model = AdImpression
    extra = 0
    readonly_fields = ['user', 'ip_address', 'created_at']
    can_delete = False


@admin.register(AdSpace)
class AdSpaceAdmin(admin.ModelAdmin):
    list_display = ['name', 'position', 'width', 'height', 'base_price', 'is_active', 'max_ads']
    list_filter = ['is_active', 'position']
    search_fields = ['name', 'description']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'position', 'description')
        }),
        ('尺寸设置', {
            'fields': ('width', 'height')
        }),
        ('价格设置', {
            'fields': ('base_price', 'discount_rate')
        }),
        ('状态设置', {
            'fields': ('is_active', 'max_ads')
        })
    )


@admin.register(Advertisement)
class AdvertisementAdmin(admin.ModelAdmin):
    list_display = ['title', 'ad_space', 'advertiser', 'status', 'start_date', 'end_date', 'view_count', 'click_count']
    list_filter = ['status', 'ad_space', 'created_at']
    search_fields = ['title', 'advertiser__email', 'description']
    readonly_fields = ['view_count', 'click_count', 'click_through_rate', 'created_at']
    inlines = [AdClickInline, AdImpressionInline]
    
    fieldsets = (
        ('基本信息', {
            'fields': ('title', 'ad_space', 'advertiser')
        }),
        ('广告内容', {
            'fields': ('image', 'link_url', 'alt_text', 'description')
        }),
        ('投放时间', {
            'fields': ('start_date', 'end_date')
        }),
        ('状态', {
            'fields': ('status', 'priority', 'reviewed_by', 'reviewed_at', 'review_note')
        }),
        ('统计信息', {
            'fields': ('view_count', 'click_count', 'click_through_rate'),
            'classes': ('collapse',)
        }),
        ('预算设置', {
            'fields': ('daily_budget', 'total_budget', 'spent_amount'),
            'classes': ('collapse',)
        })
    )


@admin.register(AdClick)
class AdClickAdmin(admin.ModelAdmin):
    list_display = ['advertisement', 'user', 'ip_address', 'created_at']
    list_filter = ['created_at']
    search_fields = ['advertisement__title', 'ip_address']
    readonly_fields = ['advertisement', 'user', 'created_at']
    
    def has_add_permission(self, request):
        return False


@admin.register(AdImpression)
class AdImpressionAdmin(admin.ModelAdmin):
    list_display = ['advertisement', 'user', 'ip_address', 'created_at']
    list_filter = ['created_at']
    search_fields = ['advertisement__title', 'ip_address']
    readonly_fields = ['advertisement', 'user', 'created_at']
    
    def has_add_permission(self, request):
        return False


@admin.register(AdCampaign)
class AdCampaignAdmin(admin.ModelAdmin):
    list_display = ['name', 'advertiser', 'status', 'start_date', 'end_date', 'total_impressions', 'total_clicks']
    list_filter = ['status', 'created_at']
    search_fields = ['name', 'advertiser__email', 'description']
    readonly_fields = ['total_impressions', 'total_clicks', 'total_spent', 'created_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'advertiser', 'description', 'hypothesis')
        }),
        ('时间设置', {
            'fields': ('start_date', 'end_date')
        }),
        ('状态', {
            'fields': ('status',)
        }),
        ('预算设置', {
            'fields': ('total_budget', 'daily_budget')
        }),
        ('统计信息', {
            'fields': ('total_impressions', 'total_clicks', 'total_spent'),
            'classes': ('collapse',)
        })
    )


@admin.register(AdPricing)
class AdPricingAdmin(admin.ModelAdmin):
    list_display = ['ad_space', 'price_type', 'base_price', 'is_active', 'valid_from', 'valid_to']
    list_filter = ['price_type', 'is_active', 'valid_from']
    search_fields = ['ad_space__name']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('ad_space', 'price_type', 'base_price')
        }),
        ('时段定价', {
            'fields': ('peak_multiplier', 'off_peak_multiplier')
        }),
        ('有效期', {
            'fields': ('valid_from', 'valid_to', 'is_active')
        })
    )
